import request from 'umi-request';
import { getStorageItem, setStorageItem } from './Storage';

const PROVINCE_LIST = 'provinceList';

const updateProvinceList = async () => {
  try {
    const response = await request.get('/api/dict/list/province');
    setStorageItem(PROVINCE_LIST, response.object);
    global.console.log('province list is loaded');
  } catch (error) {
    console.error('Failed to update province list:', error);
  }
};

const getProvinceList = () => getStorageItem(PROVINCE_LIST) || [];

const getProvinceMap = () => new Map(getStorageItem(PROVINCE_LIST)
  .map((item) => [item.key, item.value])) || new Map();

export { updateProvinceList, getProvinceList, getProvinceMap };
